package net.mikoo.seals.api.model.devices;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.Seal;

public class DevicesPositionResponse extends BaseResponse {

	private static final long serialVersionUID = 5797696876654811466L;

	private int positionType;
	
	private long partyId;
	
	private String partyName;
	
	private String partyPosition;
	
	private String expireTime;

	public int getPositionType() {
		return positionType;
	}

	public void setPositionType(int positionType) {
		this.positionType = positionType;
	}

	public long getPartyId() {
		return partyId;
	}

	public void setPartyId(long partyId) {
		this.partyId = partyId;
	}

	public String getPartyName() {
		return partyName;
	}

	public void setPartyName(String partyName) {
		this.partyName = partyName;
	}

	public String getPartyPosition() {
		return partyPosition;
	}

	public void setPartyPosition(String partyPosition) {
		this.partyPosition = partyPosition;
	}
	
	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}

	public void setResultData(Seal seal, DevicesTake take) {
		
		if(seal == null) {
			throw new IllegalArgumentException("seal");
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		if(take != null) {
			this.positionType = PositionType.TAKE.getValue();
			if(take.getPerson() != null) {
				this.partyId = take.getPerson().getPartyId();
				this.partyName = take.getPerson().getName();
			}
			this.expireTime = sdf.format(take.getEndDate());
			return ;
		}
		
		long currentTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();
		if(seal.getInterimPartyId() != null && 
				seal.getInterimExpiredDate() != null && 
				seal.getInterimExpiredDate().getTime() >= currentTime) {
			this.positionType = PositionType.INTERIM.getValue();
			this.partyId = seal.getInterimPartyId();
			this.partyName = seal.getInterimPartyName();
			this.expireTime = sdf.format(seal.getInterimExpiredDate());
			return ;
		}
		
		this.positionType = PositionType.CUSTOS.getValue();
		this.partyId = seal.getPartyId();
		this.partyName = seal.getCustosName();
	}
	
	enum PositionType{
		
		CUSTOS(1),
		
		INTERIM(2),
		
		TAKE(3);
		
		private PositionType(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}



		private int value;
		
	}
}
